<?php

	namespace App\Models\Staff;

	use App\Consts\TableConsts\TableConsts;
    use App\Func\TokenFuc;
    use App\Models\Auth\RoleModel;
    use App\Models\Staff\cStaffPermission;

    /* 职员与角色id的绑定关系
     * 
     * by breezeer at 2022.04.27
     *
     *
     *
     */
    class cStaffRole{
        
        public function __construct(){
            
            $this->m_cHelper	= app()->gcHelper				;
            $this->m_cDb		= $this->m_cHelper->gfDbQuery()	;
        }


		/* 删除角色
		 *
		 */
		public function revokeRoleName($j){
			
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere		(
																			$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')
																			,
																			[' where role_id=?',[$j->role_id]]
																			,
																			[
																				'status'	=> 2,
																			]
			)->select (
						"

							select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')." where role_id=?
						"
						,[$j->role_id]
			)->forEachRow(function($row){

								return $row->getOneRowAllData();
			},$ar);

			#
			#
			return $ar['content'];
		}

		/* 修改角色
		 *
		 */
		public function renameRoleName($j){

			# role id and role name ok ?
			#
			#
			{
				if($j->role_id>0/* role id type  must be integer */)
				{
				}
				else
				{
					return -2;
				}

				# role id exist ?
				#
				$id_exist	= $this->m_cHelper->gfDbQuery()->getDbValue("select role_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')." where role_id=?",[$j->role_id]);if($id_exist==''/* id no exist */){

					return -1;
				}else if($id_exist==$j->role_id){
					
					# 角色id 验证ok，继续检查准备修改的角色名是否已经存在 ?
					#
					if($this->m_cHelper->gfDbQuery()->getDbValue("select role_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')." where role_name=?",[$j->role_name])==''/* passed */){}else{
					
						# error => role name have exist in role configuration table
						#
						return -3;
					}
				}
			}

			# update
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere		(
																			$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')
																			,
																			[' where role_id=?',[$j->role_id]]
																			,
																			[
																				'role_name'	=> $j->role_name,
																			]
																		);

			return	[[
						'role_id'	=> $j->role_id		,
						'role_name'	=> $j->role_name	,
					]];
		}
		
		/* 创建角色
		 *
		 */
		public function createRoleName($j){
			
			# role name exist ?
			# 
			#
			$id_exist	= $this->m_cHelper->gfDbQuery()->getDbValue("select role_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')." where role_name=?",[$j->role_name]);if($id_exist==''/* role name not exist */){
				
				# ok,passed 
				#
			}else{
				
				# error,role name existed
				#
				return false;
			}

			$id = $this->m_cHelper->gfDbQuery()->insertByArray	(
																	$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')
																	,
																	[
																		'role_name'		=> $j->role_name											,
																		'company_id'	=> $this->m_cHelper->gfGetCurrentStaffInfo()->company_id	,
                                                                        # 新增了一个创建人id
                                                                        'owner_id'       => TokenFuc::getCurrentStaffInfo()['staff_id']
																	]
																)->getLastInsertId();

			return	[[
						'role_id'	=> $id				,
						'role_name'	=> $j->role_name	,
					]];
		}
		  

        /* 设置当前职员的信息
         *
         * @parameter $ar_staff_info    => Array	(
														[company_id]    => 66
														[staff_id]      => 46
													)
         * 
         *
         */
        public function setStaffInfo($ar_staff_info)
		{
            $this->m_arStafffInfo = $ar_staff_info;
            return $this;
        }
		
        /* 获取职员的全部角色
         *
         */
        public function getStaffAllRole(){
            
            return $this->m_cDb->select	("
												select 
														* 
														,b.role_name
												from 
														".$this->m_cHelper->gfGetTableName(TableConsts::CY_STAFF_ROLE_TABLE_NAME)." as a
												left join
														".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')."		as b
												on
													a.role_id=b.role_id
												where 
														1				= 1 
														and a.staff_id	= ?
												order by 
														a.id desc
											"
											,
											[$this->m_arStafffInfo['staff_id']]
											,debug:false
										)->getAllRowsData();
        }
		public function getstaffOwnAllRole($j)
		{
			return ['staff_all_role' => (new cStaffRole())->setStaffInfo(['staff_id'=>$j->staff_id])->getStaffAllRole()];
		}

        /* 绑定角色id到当前职员	=> 首先删除现有职员拥有的全部角色然后再添加新的角色给职员
         *
         * @parameter $ar_role  => [1,2,3]
         *
         *
         *
         *
         */
        public function addRoleToStaff($ar_role)
		{
			# 首先删除现有职员拥有的全部角色
			#
			$this->revokeAllRole();
			
			# 添加新角色给职员
			#
            foreach($ar_role as $n_role)
			{
                # 准备添加的role id 如果已经和职员staff id 绑定，直接跳过
                #
                if($this->m_cDb->select (

											"select id from ".$this->m_cHelper->gfGetTableName(TableConsts::CY_STAFF_ROLE_TABLE_NAME)." where role_id=? and staff_id=? "
											,
											[$n_role,$this->m_arStafffInfo['staff_id']]
											,
											debug:false
										)->getRowCount()===0){

											$this->m_cDb->insertByArray	(
																			"".$this->m_cHelper->gfGetTableName(TableConsts::CY_STAFF_ROLE_TABLE_NAME)."",
																			[

																				'staff_id'  => $this->m_arStafffInfo['staff_id'],
																				'role_id'   => $n_role							,
																			]
																		);
										}
            }

            return $this;
        }

        /* 撤销职员的部分角色
         * 
         */
        public function revokeRoleFromStaff($ar_role/* will be revoked role => [ 1,2,3,] */)
		{
            foreach($ar_role as $n_role)
			{
                $this->m_cDb->delete("delete from ".$this->m_cHelper->gfGetTableName(TableConsts::CY_STAFF_ROLE_TABLE_NAME)." where role_id=? and staff_id=? ",array($n_role,$this->m_arStafffInfo['staff_id']));
            }
            return $this;
        }

        /* 一次性撤销职员的全部角色
         *
         */
        public function revokeAllRole()
		{
            $this->m_cDb->delete("delete from ".$this->m_cHelper->gfGetTableName(TableConsts::CY_STAFF_ROLE_TABLE_NAME)." where staff_id=? ",array($this->m_arStafffInfo['staff_id']));
            return $this;
        }

		/* 获取全部公司 
		 *
		 */
		public function getAllCompany()
		{
			return $this->m_cDb->select("select * from ".$this->m_cHelper->gfGetTableName(TableConsts::CY_COMPANY_TABLE_NAME)."")->getAllRowsData();		
		}

		/* 获取公司ID （$nCompanyId） 所有的下一级公司
		 *
		 */
		public function getNextCompany($nCompanyId)
		{
			return $this->m_cDb->select("select * from ".$this->m_cHelper->gfGetTableName(TableConsts::CY_COMPANY_TABLE_NAME)." where company_parent_id=?",array($nCompanyId))->getAllRowsData();		
		}

		/* 通过权限 auth_code 获取职员最大数据位权限
		 *
		 * tips :
		 *			1. 同一个auth_code 可能分布在不同的角色下面
		 *			2. 同一个auth_code 在不同角色下权限数据位的值可能是不一样的
		 *			3. 把每一个数据位按或累加
		 *
		 *
		 *
		 */
		public function getStaffDataBitPermissionByAuthCode($auth_code)
		{
			$auth_code_map_total_data_bit_permission				= 0;foreach(($this->m_cHelper->toObjectEx($this->getAllAuth())->role_permission??[]) as $one_role_include_auth)
			{
				foreach(($one_role_include_auth->mapRoleIdToAuthId??[]) as $one_auth_code_and_data_bit_permission)
				{
					if($one_auth_code_and_data_bit_permission->auth_code == $auth_code)
					{
						$auth_code_map_total_data_bit_permission	= $auth_code_map_total_data_bit_permission | $one_auth_code_and_data_bit_permission->auth_value;
					}
				}
			}
			return $auth_code_map_total_data_bit_permission;
		}

		/* 获取职员的全部权限
		 *
		 */
		public function getAllAuth()
		{
			foreach($this->getStaffAllRole() as $one_role/* 获取用户的所有角色 */)
			{
		 		# 获取当前角色下的全部权限
				#
				$this->m_cDb->select
				(
					"	
						select 
								b.*
								,a.role_id
								,a.auth_value
								,(select role_name from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')." where role_id=a.role_id) as role_name

						from 
								".$this->m_cHelper->gfGetTableName(TableConsts::CY_ROLE_AUTH_TABLE_NAME)."  as a 
						left join 
								".$this->m_cHelper->gfGetTableName(TableConsts::CY_AUTH_AUTH_TABLE_NAME)."  as b 
						on 
								a.auth_id	= b.auth_id 
						where 
								a.role_id	= ?
					"
					,
					[$one_role['role_id']]
					,debug:false
				)
				->forEachRow
				(
					function($row)
					{
						return $row->appendItem	([])->getOneRowAllData();
					}
					,
					$arx
				);
				
				# 汇总当前职员的所有角色和权限
				#
				$ar['role_permission'][]= array('staffInfo'=>$this->m_arStafffInfo/* 职员信息 */,'roleId'=>$one_role['role_id']/* 角色 */, 'mapRoleIdToAuthId' =>$arx['content']/* 权限*/);
			}

			# 获取用户特殊的权限[ CY_STAFF_AUTH_TABLE_NAME ]，此权限获取不从角色继承
			# 
			$this->m_cDb->select
			(
				"	
										select 
												b.*
										from 
												".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_AUTH_TABLE_NAME')." as a
										left join
												".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')	." as b
										on
												a.auth_id=b.auth_id
										where 
												a.staff_id=?"
				,
				array($this->m_arStafffInfo['staff_id'])
			)
			->forEachRow
			(
				function($row)
				{
					return	[[
								'auth_id'		=> $row->auth_id		,
								'auth_name'		=> $row->auth_name		,
								'role_id'		=> 0					,
								'menu_id'		=> 0					,
								'auth_value'	=> 6					,
								'auth_code'		=> $row->auth_code		,
								'auth_category'	=> $row->auth_category	,
								'role_name'		=> ''					,
							]];
				}
				,
				$extra_permission
			);
			$ar['extra_permission']	= $extra_permission['content'];

			return $ar;
		}
		
		/* 获取职员全部的角色和权限
		 *
		 */
		function getStaffOwnAllRoleAndPermission(int $staff_id)
		{
			if($staff_id>0)
			{
				return	[
							'role_and_permission'	=> (new cStaffPermission())->setCurrentStaffID($staff_id)->getCurrentStaffAllPermission()	,
						];
			}
			else
			{
				return false;
			}
		}

		/* 获取职员的全部菜单	
		 *
		 */
		function getstaffOwnAllNavigationMenu(int $staff_id)
		{
			if($staff_id>0)
			{
				return	[
							'navigation_menu'		=> (new cStaffPermission())->setCurrentStaffID($staff_id)->getCurrentStaffNavigationMenu()	,
						];
			}
			else
			{
				return false;
			}
		
		}

		/* 获取全部菜单
		 *
		 */
		function getallNavigationMenu($j)
		{
			if($j->menu_id==0)
			{
				$this->m_cHelper->gfDbQuery()->select("select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." order by menu_id desc")->forEachRow
				(
					function($row)
					{
						return $row->getOneRowAllData();
					}
					,
					$ar
				);
			}
			else
			{
				$this->m_cHelper->gfDbQuery()->select("select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." where menu_id=? order by menu_id desc",[$j->menu_id])->forEachRow
				(
					function($row)
					{
						return $row->getOneRowAllData();
					}
					,
					$ar
				);
			}

			#
			#
			return ['all_navigation_menu'	=> $ar['content']];
		}
		
		/* 获取全部权限项目
		 *
		 */
		function getallPermissionItem($j)
		{
			if($j->auth_id==0)
			{
				$this->m_cHelper->gfDbQuery()->select
				(
					"
						select 
								*
								,ifnull((select menu_parent_id	from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." as b where b.menu_id=a.menu_id)	,-1) as menu_parent_id 
								,ifnull((select menu_code		from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." as b where b.menu_id=a.menu_id)	,'') as menu_code 
						from 
								".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." as a
						order by a.menu_id desc
					"
				)
				->forEachRow
				(
					function($row)
					{
						return $row->getOneRowAllData();
					}
					,
					$ar
				);
			}
			else
			{
				$this->m_cHelper->gfDbQuery()->select
				(
					"
						select 
								*
								,ifnull((select menu_parent_id	from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." as b where b.menu_id=a.menu_id)	,-1) as menu_parent_id 
								,ifnull((select menu_code		from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." as b where b.menu_id=a.menu_id)	,'') as menu_code 
						from 
								".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." as a 
						where 
								a.auth_id=? 
						order by a.menu_id desc
					"
					,
					[$j->auth_id]
				)
				->forEachRow
				(
					function($row)
					{
						return $row->getOneRowAllData();
					}
					,
					$ar
				);
			}

			#
			#
			return ['all_permisssion_item'	=> $ar['content']];
		}

		/* 获取全部角色
		 *
		 */
		function getallRole($j){
            $roleModel = new RoleModel();
			if($j->role_id==0){
				$roleList = $roleModel->get();
//				$this->m_cHelper->gfDbQuery()->select("select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')." order by role_id desc")->forEachRow(function($row){
//
//					return $row->getOneRowAllData();
//				},$ar);

			}else{

                $roleList = $roleModel->where('role_id',$j->role_id)->get();
//				$this->m_cHelper->gfDbQuery()->select("select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')." where role_id=? order by role_id desc",[$j->role_id])->forEachRow(function($row){
//
//					return $row->getOneRowAllData();
//				},$ar);
			}
			#
			#
			return ['all_role'	=> $roleList->toArray()];
		}
		
		/* 添加权限到角色 => 清除现有的权限,在添加本次要添加的权限
		 *
		 */
		function addpermissionToRole($j)
		{
			if(is_array($j->added_permissions))
			{
				# 清除目前角色已有的权限
				#
				$this->m_cHelper->gfDbQuery()->delete("delete from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_AUTH_TABLE_NAME')." where role_id=?",[$j->role_id]);
			
				# add
				#
				#
				foreach($j->added_permissions as $one_permission)
				{
					# add 此权限给改角色
					#
					{
						$o = (object)$one_permission;$this->m_cHelper->gfDbQuery()->insertByArray	
						(
							$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_AUTH_TABLE_NAME')
							,
							[
								'auth_id'		=> $o->permission_id		,	# permission id
								'role_id'		=> $j->role_id				,	# role id
								'auth_value'	=> $o->permission_value		,	# permission data operation bit (权限id的数据位（read => 1 write=> 2 modify => 4 delete => 8 ))
								'status'		=> 1						,	# enable
							]
						);
					}
				}
			}

			return $this->getroleIncludedAllPermission($j->role_id);
		}
		
		/* 获取角色包含的所有权限
		 *
		 */
		function getroleIncludedAllPermission(int $role_id)
		{
			#
			#
			$this->m_cHelper->gfDbQuery()->select
			(
				"
					select 
							*
							,(select auth_category	from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')	."		where auth_id=a.auth_id) as auth_category
							,(select auth_name		from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')	."		where auth_id=a.auth_id) as auth_name
							,(select auth_code		from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')	."		where auth_id=a.auth_id) as auth_code
							,(select role_name		from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_TABLE_NAME')			."		where role_id=a.role_id) as role_name
							,(select menu_id		from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')	."		where auth_id=a.auth_id) as menu_id
							,(select menu_parent_id	from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')	." as c where c.menu_id=(select menu_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." as b where b.auth_id=a.auth_id)) as menu_parent_id
							,(select menu_code		from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')	." as c where c.menu_id=(select menu_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." as b where b.auth_id=a.auth_id)) as menu_code

					from 
							".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_AUTH_TABLE_NAME')." as a
					where 
							1			= 1
							and role_id	= ?
							and status	= 1	/* enabled */
				"
				,
				[$role_id]
			)
			->forEachRow
			(
				function($row)
				{
					return $row->removeItem(['auth_role_id','status'])->getOneRowAllData();
				}
				,
				$ar
			);

			#
			#
			return ['role_included_all_permission' => $ar['content']];
		}
		/* 删除角色中包含的权限
		 *
		 */
		function revokePermissionFromRole($j)
		{
			if(is_array($j->deleted_permission))
			{
				$b = $this->m_cHelper->gfDbQuery()->buildSqlMultiParameter($j->deleted_permission);$b->thisx->update
				(
					"	
						update
								".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_AUTH_TABLE_NAME')."
						set
								status		= 2						/* disabled						*/
						where
								1			= 1
								and role_id	= ?						/* role id						*/
								and auth_id in(".$b->parameter.")	/* will be deleted permission	*/
					"
					,$b->thisx->mergeArrayMultiValueToSingle()->merge([$j->role_id])->merge($b->value)->getLastSingleArray()
				);
				
				return $this->getroleIncludedAllPermission($j->role_id);
			}																
																		
			return false;
		}

		/* 添加角色给职员
		 *
		 */
		function addroleToStaffEx($j)
		{
			if(is_array($j->added_role_id) && $j->staff_id>0 )
			{
				return (new cStaffRole())->setStaffInfo(['company_id'=>66,'staff_id'=>$j->staff_id])->addRoleToStaff($j->added_role_id)->getStaffOwnAllRoleAndPermission($j->staff_id);
			}
			return false;
		}

		/* 删除职员的角色
		 *
		 */
		function revokeRoleFromStaffEx($j)
		{
			if(is_array($j->deleted_role_id) && $j->staff_id>0 )
			{
				return (new cStaffRole())->setStaffInfo(['company_id'=>$j->company_id,'staff_id'=>$j->staff_id])->revokeRoleFromStaff($j->deleted_role_id)->getStaffOwnAllRoleAndPermission($j->staff_id);
			}
			return false;
		}

		/* 创建导航菜单栏
		 *
		 * tips:
		 *
		 *		1. 菜单栏其实就是一个菜单权限
		 *		2. 一个菜单栏必须一对一一个菜单权限
		 *		2. 为了更加方便管理菜单栏与菜单权限的一对一关系,避免数据库出现不良或者丢失配对关系,现在由代码自动创建配对关系
		 *			2.1 创建一个菜单栏
		 *			2.1 自动创建一个菜单权限		 
		 *
		 *
		 *
		 */ 
		function addnavigationMenuItem($j)
		{
			# 创建一个菜单栏
			#
			$id						= $this->m_cHelper->gfDbQuery()->insertByArray			(
																								$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')
																								,
																								[
																									'menu_name'			=> $j->menu_name			,
																									'menu_url'			=> $j->menu_url				,
																									'menu_parent_id'	=> $j->menu_parent_id		,
																									'menu_code'			=> $j->menu_code			,
																								]
																							)->getLastInsertId();

			# 自动创建一个菜单权限
			#
			$j_for_permisssionItem	= $this->m_cHelper->gfJsonDecode('{}')->appendProperty	(
																								[
																									'auth_name'			=> $j->menu_name			,
																									'menu_id'			=> $id						,
																									'auth_code'			=> $j->menu_name.'_'.$id	,
																									'auth_category'		=> 'auth_menu'				,
																								]
																							);
			$this->addonePermissionItem($j_for_permisssionItem);

			# get detail information about  have created menu item
			#
			$this->m_cHelper->gfDbQuery()->select ("select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." where menu_id=?",[$id])->forEachRow(function($row){return $row->getOneRowAllData();},$ar);

			#
			#
			return $ar['content'];				
		}

		/* 删除导航菜单栏
		 *
		 * tips :
		 *
		 *		1. 删掉一个菜单栏
		 *		2. 自动删掉一个对应的菜单栏权限 
		 *
		 *
		 */
		function revokeNavigationMenuItem(int $menu_id)
		{
			if($menu_id>0)
			{
				# delete
				#
				$this->m_cHelper->gfDbQuery()->delete("delete from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_MENU_TABLE_NAME')." where menu_id=?",[$menu_id]); 

				# 自动删掉一个对应的菜单栏权限
				#
				$this->revokePermissionItem	(
												$this->m_cHelper->gfJsonDecode('{}')->appendProperty
												(
													[
														'permission_id'	=> $this->m_cHelper->gfDbQuery()->getDbValue("select auth_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." where menu_id=?",[$menu_id])	, # 转换菜单id到权限id
													]
												)
											);

				return ['deleted_menu_id' => $menu_id]; 
			}
			return false;
		}

		/* 创建权限项目
		 *
		 */
		function addonePermissionItem($j)
		{
			# check permission unique key code have exist ?
			#
			#
			if($this->m_cHelper->gfDbQuery()->getDbValue("select auth_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." where auth_code=?",[$j->auth_code])==''/* unique key code not exist,can be added */) 
			{
				# add new permission
				#
				$id = $this->m_cHelper->gfDbQuery()->insertByArray	
				(
					$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')
					,
					[
						'auth_name'				=> $j->auth_name											,
						'menu_id'				=> $j->menu_id												, # 权限名称
						'auth_code'				=> $j->auth_code											, # 权限code 唯一
						'auth_category'			=> $j->auth_category										, # 权限分类
						'is_switch_variable'	=> $j->is_switch_variable??2								, # 2=> 不是开关 1=是开关变量(数据位权限只取 0 或者 1)
						'company_id'			=> $this->m_cHelper->gfGetCurrentStaffInfo()->company_id	, # default => 66
					]
				)->getLastInsertId();
				
				#
				#
				$this->m_cHelper->gfDbQuery()->select("select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." where auth_id=?",[$id],debug:false)->forEachRow
				(
					function($row)
					{
						return $row->removeItem(['company_id','auth_type'])->getOneRowAllData();
					}
					,
					$ar
				);
					
				return $ar['content'];
			}
			else
			{
				return false;
			}
		}


		/* 删除权限项目
		 *
		 */
		function revokePermissionItem($j)
		{
			if($j->permission_id>0)
			{
				#
				#
				$this->m_cHelper->gfDbQuery()->delete("delete from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." where auth_id=?",[$j->permission_id]);

				#
				#
				return ['deleted_permission_id'=>$j->permission_id];
			}

			# 系统当中的权限现在禁止删除
			#
			return false;
		}


		/* 获取包含此权限code的所有职员id
		 *
		 */
		public function getAllStaffIDByAuthCode($authCode/* cPermissionAuthCode::AUTH_CODE_CREDIT_PROJECT => 'AUTH_CODE_CREDIT_PROJECT' */)
		{
			# 转换 authcode 2 authid
			#
			$auth_id		= $this->m_cHelper->gfDbQuery()->getDbValue("select auth_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_AUTH_AUTH_TABLE_NAME')." as a  where a.auth_code=?",[$authCode]);if($auth_id>0)
			{
			}
			else
			{
				$auth_id	= 0;
			}

			# 获取包含此权限code的全部角色
			#
			$this->m_cHelper->gfDbQuery()->select("select a.role_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_ROLE_AUTH_TABLE_NAME')." as a force index(auth_id) where a.auth_id=?",[$auth_id],debug:false)->forEachRow
			(
				function($row)
				{
					return $row->role_id;
				}
				,
				$ar
			);
			$all_role		= [0];if(count($ar['content'])>0)
			{
				$all_role	= $ar['content'];
			}

			# 获取包含此角色的所有职员id
			#
			$ar				= null;
			$bm				= $this->m_cHelper->gfDbQuery()->buildSqlMultiParameter($all_role);
			$this->m_cHelper->gfDbQuery()->select("select b.staff_id from cy_staff_role as b force index(staff_id_role_id)  where b.role_id in (".$bm->parameter.")",$bm->cDB->mergeArrayMultiValueToSingle($bm->value)->getLastSingleArray())->forEachRow
			(
				function($row)
				{
					return $row->staff_id;
				}
				,
				$ar
			);
			
			#
			#
			return $ar['content'];		
		}
    }